package com.bdmc.web.controller.base;

import java.util.List;
import java.util.Arrays;

import com.bdmc.base.domain.bo.BaseFencePointBo;
import com.bdmc.base.domain.vo.BaseFencePointVo;
import com.bdmc.base.service.IBaseFencePointService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.base.domain.vo.BaseFenceVo;
import com.bdmc.base.domain.bo.BaseFenceBo;
import com.bdmc.base.service.IBaseFenceService;
import com.bdmc.common.core.page.TableDataInfo;


/**
 * 网格控制器
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/fence")
public class BaseFenceController extends BaseController {

    private final IBaseFenceService iBaseFenceService;

    private final IBaseFencePointService fencePointService;
    /**
     * 查询网格列表
     */
    @SaCheckPermission("base:fence:list")
    @GetMapping("/list")
    public TableDataInfo<BaseFenceVo> list(@Validated(QueryGroup.class) BaseFenceBo bo, PageQuery pageQuery) {
        return iBaseFenceService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出网格列表
     */
    @SaCheckPermission("base:fence:export")
    @Log(title = "网格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BaseFenceBo bo, HttpServletResponse response) {
        List<BaseFenceVo> list = iBaseFenceService.queryList(bo);
        ExcelUtil.exportExcel(list, "网格", BaseFenceVo.class, response);
    }

    /**
     * 获取网格详细信息
     */
    @SaCheckPermission("base:fence:query")
    @GetMapping("/{fenceId}")
    public AjaxResult<BaseFenceVo> getInfo(@PathVariable("fenceId") Long fenceId) {
        BaseFenceVo baseFenceVo = iBaseFenceService.queryById(fenceId);
        List<BaseFencePointVo> baseFencePointList = fencePointService.queryList(new BaseFencePointBo().setFenceId(fenceId));
        if(baseFencePointList!=null && baseFencePointList.size()>0){
            baseFenceVo.setFencePointList(baseFencePointList);
        }
        return AjaxResult.success(baseFenceVo);
    }

    /**
     * 新增网格
     */
    @SaCheckPermission("base:fence:add")
    @Log(title = "网格", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BaseFenceBo bo) {
        return toAjax(iBaseFenceService.insertByBo(bo));
    }

    /**
     * 修改网格
     */
    @SaCheckPermission("base:fence:edit")
    @Log(title = "网格", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BaseFenceBo bo) {
        return toAjax(iBaseFenceService.updateByBo(bo));
    }

    /**
     * 删除网格
     */
    @SaCheckPermission("base:fence:remove")
    @Log(title = "网格" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{fenceIds}")
    public AjaxResult<Void> remove(@PathVariable Integer[] fenceIds) {
        return toAjax(iBaseFenceService.deleteWithValidByIds(Arrays.asList(fenceIds), true));
    }

    @GetMapping("combo")
    public AjaxResult<List<BaseFenceVo>> combo( BaseFenceBo bo){
        return AjaxResult.success(iBaseFenceService.queryList(bo));
    }
}
